<?php
namespace jy_ylfee;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	public function check_member(){
		$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		if(!$member['is_agent'] || $member['stop'] || !$this->setting['level']){
			$this->register();
			exi('','','jy_ylfee/apply/');
		}
		return $member;
	}
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('区域代理会员注册失败，缺少会员ID！');
		}
		$ismid=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid']),array('mid'));
		if($ismid){
			return false;
		}
		$member['mobile']=$member['mobile']?:pdo_getcolumn('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'mobile');
		$fromuser=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		$member['fromuser']=$fromuser;
		$member['status']=1;
		$boss=$this->getboss($_SESSION['uid']);
		if($boss['id'] && $boss['level_id']!=2){
			$member['province']=$boss['province'];
			$member['city']=$boss['city'];
		}
		$this->insert('member',$member);
	}
	/*
		申请成为区域代理
	*/
	public function apply_agent($applyid,$area_code='',$post=array()){
		if(!$area_code && !$post['is_duodai']){
			exi('获取区域code失败！','error');
		}
		$apply=$this->get('apply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$applyid));
		//独家代理模式下，不允许重复
		$this->check_isadd($post);
		$new_apply=array(
			'mobile'=>trim($post['mobile']),
			'realname'=>trim($post['realname']),
			'wechat'=>trim($post['wechat']),
			'status'=>0,
			'area_type'=>pdo_getcolumn('core_area',array('code'=>$area_code),'type'),
			'area_code'=>$area_code,
			'province'=>$post['province'],
			'city'=>$post['city'],
			'level_id'=>$post['level_type'],
		);
		if($apply['id']){
			$this->update('apply',$new_apply,array('uniacid'=>$_SESSION['uniacid'],'id'=>$apply['id']));
		}else{
			$new_apply['uniacid']=$_SESSION['uniacid'];
			$new_apply['mid']=$mid;
			$new_apply['createtime']=TIMESTAMP;
			$this->insert('apply',$new_apply);
			$apply['id']=$this->insertid();
		}
		return $apply['id'];
	}
	public function check_isadd($post=array()){
		$params=array('uniacid'=>$_SESSION['uniacid'],'province'=>$post['province'],'city'=>$post['city']);
		if(!$post['level_type']){
			$params['level_id']=0;
			if($this->setting['agent0num']){
				$isagent0num=pdo_count('jy_ylfee_member',$params);
				if($this->setting['agent0num']<=$isagent0num){
					exi('该市区合伙商人数已上限！请更换！','error');
				}
			}
		}elseif($post['level_type']==1){
			$params['level_id']=1;
			$isagent1num=pdo_count('jy_ylfee_member',$params);
			if($isagent1num){
				exi('该市区已存在代理！请更换！','error');
			}
		}elseif($post['level_type']==2){
			unset($params['city']);
			$params['level_id']=2;
			$isagent1num=pdo_count('jy_ylfee_member',$params);
			if($isagent1num){
				exi('该省区已存在代理！请更换！','error');
			}
		}
		return '';
	}
	public function up_agent($mid,$area_code,$post=array()){
		if(!$area_code){
			exi('请选择区域省份！','error');
		}
		$mid=$mid?:$post['mid'];
		if($area_code){
			$new_apply['area_code']	=$area_code;
			$new_apply['area_type']	=pdo_getcolumn('core_area',array('code'=>$area_code),'type');
			$new_apply['province']	=$post['area']['province'];
			$new_apply['city']	=$post['area']['city'];
		}
		$this->update('member',$new_apply,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$post['mid']));
		return $id;
	}
	/*
		审核区域代理
	*/
	public function check_agent($apply_id,$status,$remark=''){
		if(!$apply_id || !$status){
			return;
		}
		$apply=$this->get('apply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$apply_id));
		if(!$apply){
			return;
		}
		$update=array(
			'status'=>$status,
			'complete_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			$memberupdate=array(
				'status'=>1,
				'is_agent'=>1,
				'area_type'=>$apply['area_type'],
				'area_code'=>$apply['area_code'],
				'province'=>$apply['province'],
				'city'=>$apply['city'],
				'level_id'=>$apply['level_id'],
				'realname'=>$apply['realname'],
				'mobile'=>$apply['mobile'],
			);
			//审核通过，如果会员还不是代理商，此处修改
			$this->update('member',$memberupdate,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$apply['mid']));
		}else{
			$update['remark']=$remark;
		}
		$this->update('apply',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$apply_id));
		if($update['status']==1){
			$this->model('notice')->notice_fenxiao_add($member['mid']);
		}
		return true;
	}
	public function set_parent($mid,$fromuser){
		$this->update('member',array('fromuser'=>$fromuser),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		$this->model('notice')->notice_fenxiao_xiajiadd($mid,$fromuser);
		return true;
	}
	/*
		禁用代理商
	*/
	public function stop_agent($fromuser){
		$this->update('member',array('stop'=>1,'status'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser));
	}
	/*
		恢复代理商
	*/
	public function start_agent($fromuser){
		$this->update('member',array('stop'=>0,'status'=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser));
	}
	//直接开通等级
	public function up_arealevel($mid,$level_id=0){
		$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if($member['id']){
			if($level_id>$member['level_id']){
				$this->update('member',array('level_id'=>$level_id),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
			}
		}else{
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'status'=>1,
				'level_id'=>$level_id,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('member',$new_apply);
			$id=$this->insertid();
		}	
		return $id;
	}
	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($fromuser){
		$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser));
		if(!$member['mid']){
			return;
		}
		$fenset=$this->setting['fenset'];
		paixu($fenset,'key','desc');
		$up=false;
		foreach($fenset as $lv){
			if($lv['goods'] && $lv['num']){
				$goods_ids=json_decode($lv['goods'],true);
				foreach($goods_ids as $gv){
					$isbuynum=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'mid'=>$fromuser,'goodsid'=>$gv['id']),'total');
					if($isbuynum>0 && $isbuynum>$lv['num']){
						$up=true;
						break;
					}
				}
			}else{
				$up=true;
			}
			if($up){
				$next_level=$lv;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['key'],
			);
			$this->update('member',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser));
		}
		return true;
	}
	public function upfee78($member=array()){
		if(!$member['mid'] || !$member['level_id']){
			return '';
		}
		if($member['level_id']==1){
			if(!$this->setting['cityman'] || !$this->setting['cityfee']){
				return '';
			}
			$jifeeman=$this->setting['cityman'];
			$upfee=$this->setting['cityfee'];
		}
		if($member['level_id']==2){
			if(!$this->setting['shengman'] || !$this->setting['shengfee']){
				return '';
			}
			$jifeeman=$this->setting['shengman'];
			$upfee=$this->setting['shengfee'];
		}
		$isfee=pdo_count('jy_ylfee_commission',array('level'=>$member['level_id']==1?7:8,'uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid']));
		if($isfee){
			return '';
		}
		$yueend =time();
		$benfee4=$this->model('commission')->onememberfee(4,$member,array(0,$yueend));
		if($benfee4['jifee']>=$jifeeman){
			$new_commission=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$member['mid'],
				'fromuser'=>$member['id'],
				'area_code'=>$member['area_code'],
				'jifee'=>$jifeeman,
				'month'=>'',
				'level'=>$member['level_id']==1?7:8,
				'money'=>$upfee,
				'createtime'=>TIMESTAMP,
				'status'=>1,
			);
			$this->insert('commission',$new_commission);
		}
		return '';
	}
	public function checkapptype($isinfo=false){
		$member=$this->getboss($_SESSION['uid']);
		$level_id=$member['level_id'];
		if($level_id<=1){
			//只能该市下面的合伙人
			$apptype=1;
		}
		if(!$member['mid']){
			$apptype=0;
			pdo_update('core_members',array('fromuser'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']));
			pdo_update('jy_ylfee_member',array('fromuser'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		}
		if($level_id==2){
			$apptype=2;
		}
		if($isinfo){
			return array('apptype'=>$apptype,'boss'=>$member);
		}else{
			return $apptype;
		}
	}
	public function getboss($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$fromuser=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'fromuser');
		for($i=1;$i<19;$i++){
			if(!$fromuser){
				break;
			}
			$member=pdo_get('jy_ylfee_member',array('uniacid'=>$_SESSION['uniacid'],'is_agent'=>1,'mid'=>$fromuser));
			if($member['level_id']){
				break;
			}
			$fromuser=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$fromuser),'fromuser');
		}
		return $member;
	}
	public function getteamids($teammid,$nomy=false){
		$newmembers=array();
		$newteammids=$teammid;
		for($i=1;$i<9;$i++){
		    $params=array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$newteammids);
			$members=pdo_getall('core_members',$params,array('id'),'id');
			if($members){
				foreach($members as $key=>$item){
					$newmembers[$key]=$key;
				}
				$newteammids=array_keys($members);
			}else{
				break;
			}
		}
		if($newmembers){
			$teamids=array_keys($newmembers);
		}else{
			$teamids=$nomy?array():array($teammid);
		}
		return $teamids;
	}
}